var _util = require('../util/util.js');
var _product_service = {
    //通过id获取商品的属性
    getOneItem: function (itemid, resolve, reject) {
        _util.request({
            url: _util.getServerURL('/catalog/items/' + itemid + '/attributes'),
            method: 'GET',
            success: resolve,
            error: reject
        });
    },
    //获取商品的二级分类
    getProducts: function (productid, resolve, reject) {
        _util.request({
            url: _util.getServerURL('/catalog/categories/' + productid),
            method: 'GET',
            success: resolve,
            error: reject
        });
    },
    //获取轮播图
    getSwiperPic: function (resolve, reject) {
        _util.request({
            url: _util.getServerURL('/catalog/swiper-items/'),
            method: 'GET',
            success: resolve,
            error: reject
        })
    },
    //获取某个店铺全部商品数据
    getItemsByShopId: function (shopid, sortby, resolve, reject) {
        let urlStr = '/catalog/items/shops/' + shopid + '?sortby=' + sortby;
        _util.request({
            url: _util.getServerURL(urlStr),
            method: 'GET',
            success: resolve,
            error: reject
        })
    },
    //获取某个店铺全部商品数据(分页)
    getItemsPage: function (shopid, pagesize, pageid, sortby, resolve, reject) {
        let urlStr = '/catalog/items/shops/' + shopid + '/pages/' + pagesize + '/' + pageid + '?sortby=' + sortby;
        _util.request({
            url: _util.getServerURL(urlStr),
            method: 'GET',
            success: resolve,
            error: reject
        })
    },
    //修改商品信息
    updateItem: function (itemid, stock, price, description, resolve, reject) {
        _util.request({
            url: _util.getServerURL('/catalog/items/' + itemid),
            method: 'PUT',
            data: { stock, price, description },
            success: resolve,
            error: reject
        })
    },
    //处理商品上架与下架
    manageItem: function (itemid, available, resolve, reject) {
        _util.request({
            url: _util.getServerURL('/catalog/items/' + itemid + '/available?available=' + available),
            method: 'PUT',
            success: resolve,
            error: reject
        })
    },
    insertRecord: function (itemid, resolve, reject) {
        _util.request({
            url: _util.getServerURL('/catalog/records'),
            method: "POST",
            success: resolve,
            error: reject,
            data: {
                itemid: itemid
            }
        });
    },
    getRecords: function (resolve, reject) {
        _util.request({
            url: _util.getServerURL('/catalog/records'),
            method: "GET",
            success: resolve,
            error: reject
        });
    },

    //通过id获取某商品的具体信息
    getOneItemInfo: function (itemid, resolve, reject) {
        _util.request({
            url: _util.getServerURL('/catalog/items/' + itemid),
            method: 'GET',
            success: resolve,
            error: reject
        });
    },
    //查询销量前N的商品
    getPopularItems: function (num, resolve, reject) {
        _util.request({
            url: _util.getServerURL('/catalog/items/popular?num=' + num),
            method: 'GET',
            success: resolve,
            error: reject
        })
    },
}
module.exports = _product_service;